package com.roadmap.struts.action;

import javax.servlet.http.HttpServletRequest;

import com.roadmap.common.BaseConst;

public class PagingInfo {
	private int totalResultCount;
	private int currentPageNum;
	private int firstPageNum;
	private int lastPageNum;
	private int totalPageCount = -1; // 设置一个不合法的值，根据该值来判断是否已赋值
	private String preparedURL;

	private final static int FIRST_OFFSET_CURRENT = 2;
	private final static int FIRST_OFFSET_LAST = 4;

	public PagingInfo(HttpServletRequest request, int currentPageNum, int totalResultCount) {
		this.currentPageNum = currentPageNum;
		this.totalResultCount = totalResultCount;
		this.preparedURL = prepareURL(request);
	}

	public int getTotalResultCount() {
		return totalResultCount;
	}

	public void setTotalResultCount(int totalResultCount) {
		this.totalResultCount = totalResultCount;
	}

	public int getCurrentPageNum() {
		if (this.currentPageNum < 1) {
			this.currentPageNum = 1;
		}

		return this.currentPageNum;
	}

	public void setCurrentPageNum(int currentPageNum) {
		this.currentPageNum = currentPageNum;
	}

	public int getTotalPageCount() {
		if (this.totalPageCount == -1) {
			this.totalPageCount = this.totalResultCount
					/ BaseConst.MAX_RESULTS_IN_QUERY;
			if (this.totalResultCount % BaseConst.MAX_RESULTS_IN_QUERY != 0) {
				this.totalPageCount++;
			}
		}

		return this.totalPageCount;
	}

	public int getFirstPageNum() {
		this.firstPageNum = this.currentPageNum - FIRST_OFFSET_CURRENT;
		if (this.firstPageNum < 1) {
			this.firstPageNum = 1;
		}

		if (this.firstPageNum > getTotalPageCount()) {
			this.firstPageNum = getTotalPageCount();
		}

		return this.firstPageNum;
	}

	public int getLastPageNum() {
		this.lastPageNum = getFirstPageNum() + FIRST_OFFSET_LAST;
		if (this.lastPageNum > getTotalPageCount()) {
			this.lastPageNum = getTotalPageCount();
		}
		return this.lastPageNum;
	}
	
	public String getPreparedURL() {
		return preparedURL;
	}

	/** 判断分页数据是否合理 */
	public boolean isOK() {
		return this.currentPageNum > 0 && getTotalPageCount() > 0
				&& this.currentPageNum <= getTotalPageCount();
	}

	private String prepareURL(HttpServletRequest request) {
		if (request == null) {
			return null;
		}

		StringBuffer requestUrl = request.getRequestURL();
		requestUrl.append("?");
		String queryString = request.getQueryString();
		if (queryString == null) {
			requestUrl.append("p=");
		}
		else {
			// remove the parameter 'p' from the query string
			int index = queryString.indexOf("p=");
			if(index == -1) {
				requestUrl.append(queryString);
				requestUrl.append("&p=");
			}
			else {
				int i = index;
				for (; i < queryString.length(); i++) {
					char ch = queryString.charAt(i);
					if (ch == '&') {
						i++;
						break;
					}
				}

				String sub = queryString.substring(index, i);
				requestUrl.append(queryString.replace(sub, ""));
				requestUrl.append((i < queryString.length() ? "&p=" : "p="));
			}
		}
		return requestUrl.toString();
	}
	
	public static void main(String[] agrs) {
		PagingInfo pf = new PagingInfo(null, 30, 200);

		System.out.println("current: " + pf.getCurrentPageNum());
		System.out.println("total: " + pf.getTotalPageCount());
		System.out.println("first: " + pf.getFirstPageNum());
		System.out.println("last: " + pf.getLastPageNum());
	}
}
